package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mbyte.easy.admin.entity.Draft;
import com.mbyte.easy.admin.service.IDraftService;
import com.mbyte.easy.admin.service.IGroupService;
import com.mbyte.easy.admin.service.IPptService;
import com.mbyte.easy.common.constant.DraftType;
import com.mbyte.easy.common.constant.StateDic;
import com.mbyte.easy.common.constant.SysRole;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassNameDraftFinalPassController
 * @Description 稿件终审人
 * @Author myr
 * @Date 2019/12/16 20:09
 * @Version 1.0
 **/
@Slf4j
@Controller
@RequestMapping("/draft/draftFinal")
public class DraftFinalPassController extends BaseController {

    private String prefix = "admin/draft/draftFinal/";

    @Autowired
    IDraftService draftService;

    @Autowired
    IPptService pptService;

    @Autowired
    IGroupService groupService;

    /**
     * @return
     * @Description //稿件终审人审核首页--主/7
     * @Author myr
     **/
    @RequestMapping("finalCheckIndex")
    public String finalCheckIndex(Model model, Draft draft, String createTimeSpace,
                                  @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                  String uuid) {

        model.addAttribute("searchInfo", draft);
        model.addAttribute("pageInfo", new PageInfo(draftService.finalCheckIndex(draft, pageNo, pageSize, createTimeSpace)));
        //获得组内稿件总数目
        QueryWrapper<Draft> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id", draft.getGroupId());
        List<Draft> drafts = draftService.list(queryWrapper);

        Map map = draftService.setFinalGroupStatus(draft.getStatusCode());

        model.addAttribute("roleId",draftService.selectRoleId());
        model.addAttribute("draftGroup", map);//稿件分组
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("draftSize", drafts.size());
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        model.addAttribute("stateDic", StateDic.stateDic());
        return prefix + "finalCheckIndex";
    }

    /**
     * @param draftGroup 稿件组别
     * @return
     * @Description 稿件终审人抽查通过
     * @Author myr
     **/
    @PostMapping("check")
    @ResponseBody
    public AjaxResult check(String draftGroup) {
        log.info("the draftGroup is --> {}", draftGroup);
        return toAjax(draftService.finalCheckPass(draftGroup));
    }


    /**
     * @return
     * @Description //稿件终审人审核成功
     * 已入库
     * @Author myr
     **/
    @RequestMapping("checkSuccess")
    public String checkSuccess(Model model, Draft draft,
                               @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("roleId",draftService.selectRoleId());
        model.addAttribute("pageInfo", new PageInfo(draftService.finalCheckIndexSuccess(draft, pageNo, pageSize)));
        model.addAttribute("draftGroup", draftService.setFinalGroupStatus(draft.getStatusCode()));//稿件分组
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        model.addAttribute("stateDic", StateDic.stateDic());
        if(draft.getDraftType() == null){
            draft.setDraftType(StateDic.NULL);
        }
        if(draft.getDraftType() == DraftType.SVIDEO){
            return prefix + "finalVideoCheckSuccess";
        }
        return prefix + "finalCheckSuccess";
    }

    /**
     * @return
     * @Description //稿件终审人审核驳回
     * 主 / 6 / 7
     * @Author myr
     **/
    @RequestMapping("checkFalse")
    public String checkFalse(Model model, Draft draft,
                             @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("pageInfo", new PageInfo(draftService.finalCheckIndexFalse(draft, pageNo, pageSize)));
        model.addAttribute("draftGroup", draftService.setFinalGroupStatus(draft.getStatusCode()));//稿件分组
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("roleId",draftService.selectRoleId());
        return prefix + "finalCheckFalse";
    }

//    /**
//     * @Description  稿件终审人抽查驳回
//     * @param draftId 稿件组别
//     * @Author myr
//     * @return
//     **/
//    @PostMapping("disallow")
//    @ResponseBody
//    public AjaxResult disallow(String draftId,String disAllowdReason){
//        log.info("the draftGroup is --> {}",draftId);
//        return toAjax(draftService.finalCheckDisallow(Long.valueOf(draftId),disAllowdReason));
//    }

    /**
     * @param draft 稿件
     * @return
     * @Description 稿件驳回工具类
     * @Author myr
     **/
    @PostMapping("disallowUtil")
    @ResponseBody
    public AjaxResult disallowUtil(Long id, String disAllowdReason, Draft draft) {
        return toAjax(draftService.disallowUtil(id, disAllowdReason, draft));
    }

    @PostMapping("fileOut")
    @ResponseBody
    public AjaxResult fileOut(@RequestParam("draftText") String draftText) throws IOException {
        log.info("进来了===========");
        String content = "<html><head><head><body>"+draftText+"</body></html>";
        InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"));
        OutputStream os =  new FileOutputStream("C:\\Users\\44312\\Desktop\\导出稿件");
        POIFSFileSystem fs = new POIFSFileSystem();
        //对应于org.apache.poi.hdf.extractor.WordDocument
        fs.createDocument(is, "WordDocument");
        fs.writeFilesystem(os);
        os.close();
        is.close();
        return AjaxResult.success();
    }

    /**
     * @param draftId 稿件id
     * @return
     * @Description 稿件审核通过工具类
     * @Author myr
     **/
    @PostMapping("passUtil")
    @ResponseBody
    public AjaxResult passUtil(String draftId, Draft draftPre) {
        return toAjax(draftService.passUtil(Long.valueOf(draftId), draftPre));
    }

    /**
     * @return
     * @Description //稿件驳回列表首页
     * @Author myr
     **/
    @RequestMapping("disallowIndex")
    public String disallowIndex(Model model, Draft draft, String createTimeSpace,
                                @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("pageInfo", new PageInfo(draftService.disallowIndex(draft, pageNo, pageSize, createTimeSpace)));
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        return prefix + "disallowIndex";
    }


    @RequestMapping("disallowVideoIndex")
    public String disallowVideoIndex(Model model, Draft draft, String createTimeSpace,
                                @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("pageInfo", new PageInfo(draftService.disallowVideoIndex(draft, pageNo, pageSize, createTimeSpace)));
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        return prefix + "disallowIndex-video";
    }

    /**
     * @return
     * @Author 张雪
     * @Description 板换负责人--被驳回稿件
     * @Date 2020/1/9 12:34
     * @Param
     **/
    @RequestMapping("blockFail")
    public String blockFail(Model model, Draft draft, String createTimeSpace,
                            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {

        model.addAttribute("searchInfo", draft);
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("pageInfo", new PageInfo(draftService.disallowIndex(draft, pageNo, pageSize, createTimeSpace)));
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        return prefix + "disallowIndex";
    }


    /**
     * @return
     * @Description //写手稿件驳回列表首页
     * @Author myr
     **/
    @RequestMapping("diswriter")
    public String diswriter(Model model, Draft draft, String createTimeSpace,
                            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("pageInfo", new PageInfo(draftService.disallowIndex(draft, pageNo, pageSize, createTimeSpace)));
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显

        return prefix + "diswriter";
    }

    /**
     * @return
     * @Description //板块负责人已驳回
     * @Author myr
     **/
    @RequestMapping("disallow")
    public String disallow(Model model, Draft draft, String createTimeSpace,
                           @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("pageInfo", new PageInfo(draftService.disallow(draft, pageNo, pageSize, createTimeSpace)));
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        return prefix + "disallow";
    }

    @RequestMapping("disallowVideo")
    public String disallowVideo(Model model, Draft draft, String createTimeSpace,
                           @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("pageInfo", new PageInfo(draftService.disallowVideo(draft, pageNo, pageSize, createTimeSpace)));
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        return prefix + "disallow-video";
    }

    /**
     * @return
     * @Description //稿件待审核首页
     *  主/6/7--板块负责人   6--稿件终审人
     * @Author myr
     **/
    @RequestMapping("waitCheckIndex")
    public String waitCheckIndex(Model model, Draft draft, String createTimeSpace,
                                 @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("pageInfo", new PageInfo(draftService.waitCheckIndex(draft, pageNo, pageSize, createTimeSpace)));
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        if (draftService.selectRoleId() == SysRole.Draft_Final_Reviewer){
            return prefix + "finalWaitSix";
        }
        model.addAttribute("roleId",draftService.selectRoleId());
        return prefix + "blockWait";
    }

    /**
     * @return
     * @Description //稿件待审核首页
     *  主/6/7--板块负责人   6--稿件终审人
     * @Author myr
     **/
    @RequestMapping("waitCheckVideoIndex")
    public String waitCheckVideoIndex(Model model, Draft draft, String createTimeSpace,
                                 @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("pageInfo", new PageInfo(draftService.waitCheckVideoIndex(draft, pageNo, pageSize, createTimeSpace)));
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("draftTypes", draftService.setDraftType());//稿件类型回显
        model.addAttribute("draftCheckStatus", draftService.setDraftCheckStatus());//稿件审核状态回显
        if (draftService.selectRoleId() == SysRole.Draft_Final_Reviewer){
            return prefix + "finalWaitSix";
        }
        model.addAttribute("roleId",draftService.selectRoleId());
        return prefix + "blockVideoWait";
    }


    /**
     * 稿件类型数据回显
     *
     * @param model
     */
    public void setDraftType(Model model) {
        Map<Integer, String> types = new HashMap<Integer, String>();
        types.put(DraftType.Image_Text, "图文");
        types.put(DraftType.QA, "问答");
        types.put(DraftType.PPT, "PPT");
        types.put(DraftType.Audio, "音频");
        model.addAttribute("draftTypes", types);
    }
    /**
     * @Author 张雪
     * @Description //稿件终审人批量通过 6队
     * @Date 2020/2/14 20:42
     * @Param
     * @return
     **/
    @PostMapping("checkSix")
    @ResponseBody
    public AjaxResult checkSix(String ids) {
        return toAjax(draftService.checkSix(ids));
    }

    @RequestMapping("sevenVideo")
    public String sevenVideo(Model model, Draft draft, String createTimeSpace,
                                      @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        model.addAttribute("searchInfo", draft);
        model.addAttribute("pageInfo",new PageInfo(draftService.sevenVideo(draft,pageNo,pageSize,createTimeSpace)));
        model.addAttribute("time", createTimeSpace);
        model.addAttribute("stateDic", StateDic.stateDic());
        model.addAttribute("code",draft.getStatusCode());
        return prefix+"sevenvideo";
    }
}
